﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using Entity;
using BLL;

namespace Web.admin.OutterManage
{
    public partial class AddOutterPhotos : System.Web.UI.Page
    {
        private OutterPhotosSevice photoService = new OutterPhotosSevice();

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                string operation = Request["PARTICIPANTS"];
                if (operation == null)
                {
                    string partId = Request["ParticipantsId"];
                    ParticipantsId.Value = partId;
                    FileTR.Visible = true;
                    return;
                }
                string pictureId = Request["id"];
                if (pictureId == null)
                    return;

                if (operation == "Modify")
                {
                    ModifyPicture(pictureId);
                }
                else if (operation == "Delete")
                {
                    DeletePicture(pictureId);
                }
            }
        }

        protected void ModifyPicture(string pictureId)
        {
            PictureId.Value = pictureId;

            BoOutterPhotos photo = photoService.getPhoto(pictureId);

            txtTitle.Text = photo.OPHO_TITLE;
            txtDate.Text = photo.OPHO_DATE.ToString();
            Image1.ImageUrl = "/UploadImages/" + photo.OPHO_URL;

            FileTR.Visible = false;
        }

        protected void DeletePicture(string pictureId)
        {
            BoOutterPhotos photo = photoService.getPhoto(pictureId);
            DeleteImageFile(photo);
            photoService.DeletePhoto(photo);
            Response.Redirect("OutterPictures.aspx?id=" + photo.OPHO_PARTICIPANTS);
        }

        protected void btn_Save_Click(object sender, EventArgs e)
        {
            BoOutterPhotos photo = null;
            if (PictureId.Value == "")
            {
                photo = new BoOutterPhotos();
                photo.OPHO_PARTICIPANTS = ParticipantsId.Value;
            }
            else
                photo = photoService.getPhoto(PictureId.Value);

            photo.OPHO_TITLE = txtTitle.Text;
            photo.OPHO_DATE = DateTime.Parse(txtDate.Text);

            if (PictureId.Value == "")
            {
                photo.OPHO_URL = SaveImageFile();
                photoService.CreatPhoto(photo);
            }
            else
                photoService.ModifyPhoto(photo);

            Response.Redirect("OutterPictures.aspx?id=" + photo.OPHO_PARTICIPANTS);
        }

        public String SaveImageFile()
        {
            Boolean fileOK = false;
            String path = Server.MapPath("~/UploadImages/");
            if (FileUpload1.HasFile)
            {
                String fileExtension = System.IO.Path.GetExtension(FileUpload1.FileName).ToLower();
                String[] allowedExtensions = { ".gif", ".png", ".jpeg", ".jpg" };
                for (int i = 0; i < allowedExtensions.Length; i++)
                {
                    if (fileExtension == allowedExtensions[i])
                    {
                        fileOK = true;
                    }
                }
            }
            if (fileOK)
            {
                try
                {
                    String pre = Guid.NewGuid().ToString();
                    FileUpload1.PostedFile.SaveAs(path + pre + "_" + FileUpload1.FileName);
                    return pre + "_" + FileUpload1.FileName;
                }
                catch (Exception ex)
                {
                    throw new Exception("图像文件保存失败！：" + ex.Message);
                }
            }
            else
            {
                throw new Exception("不支持的图像文件格式");
            }
        }

        public void DeleteImageFile(BoOutterPhotos photo)
        {
            String path = Server.MapPath("~/UploadImages/");
            String imagePath = path + photo.OPHO_URL;
            System.IO.File.Delete(imagePath);
        }
    }
}